﻿using WWLauncher.Constant;
using System;
using System.IO;
using System.Text;

namespace WWLauncher.Util
{
    public class LogUtil
    {
        public static void WriteErrorLog(Exception ex, string strmsg = "")
        {
            try
            {
                if (!Directory.Exists(Constants.ErrorFilePath.Substring(0, Constants.ErrorFilePath.LastIndexOf("\\"))))
                {
                    Directory.CreateDirectory(Constants.ErrorFilePath.Substring(0, Constants.ErrorFilePath.LastIndexOf("\\")));
                }
                StringBuilder msg = new StringBuilder();
                msg.Append("*************************************** \r\n");
                msg.AppendFormat(" 异常发生时间： {0} \r\n", DateTime.Now);
                msg.AppendFormat(" 异常类型： {0} \r\n", ex.HResult);
                msg.AppendFormat(" 导致当前异常的 Exception 实例： {0} \r\n", ex.InnerException);
                msg.AppendFormat(" 导致异常的应用程序或对象的名称： {0} \r\n", ex.Source);
                msg.AppendFormat(" 引发异常的方法： {0} \r\n", ex.TargetSite);
                msg.AppendFormat(" 异常堆栈信息： {0} \r\n", ex.StackTrace);
                msg.AppendFormat(" 异常消息： {0} \r\n", ex.Message);
                msg.Append("***************************************");

                try
                {
                    if (File.Exists(Constants.ErrorFilePath))//如果文件存在
                    {
                        //写异常信息写入文件
                        using (StreamWriter tw = File.AppendText(Constants.ErrorFilePath))
                        {
                            tw.WriteLine(msg.ToString());
                        }
                    }
                    else
                    {
                        TextWriter tw = new StreamWriter(Constants.ErrorFilePath);
                        tw.WriteLine(msg.ToString());
                        tw.Flush();
                        tw.Close();
                        tw = null;
                    }
                }
                catch (Exception exx)
                {
                    Console.ReadKey();
                }
            }
            catch
            {

            }

        }


        public static void WriteLog(string msg)
        {
            try
            {
                if (!Directory.Exists(Constants.LogFilePath.Substring(0, Constants.LogFilePath.LastIndexOf("\\"))))
                {
                    Directory.CreateDirectory(Constants.LogFilePath.Substring(0, Constants.LogFilePath.LastIndexOf("\\")));
                }
                using (FileStream fs = File.Open(Constants.LogFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    fs.Seek(0, SeekOrigin.End);
                    byte[] buffer = Encoding.UTF8.GetBytes("-------------------------------------------------------\r\n");
                    fs.Write(buffer, 0, buffer.Length);

                    buffer = Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + msg + "\r\n");
                    fs.Write(buffer, 0, buffer.Length);
                }
            }
            catch { }
        }


    }
}
